<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:f="livcos.org/ns/xslt/function"
	exclude-result-prefixes="f">

	<xsl:import href="base.xsl" />
	
	<xsl:template match="/">
		<xsl:variable name="loginUrl" select="'https://www.google.com/accounts/ClientLogin'"/>
		<xsl:variable name="feedUrl" select="'http://www.google.com/m8/feeds/contacts/default/full'"/>
		<xsl:variable name="userLogin" select="$testLogins[@id = 'login_6']"/>
		<xsl:variable name="loginRequest">
			<http-request method="POST">
				<header name="Content-Type">application/x-www-form-urlencoded</header>
				<param name="Email"><xsl:value-of select="$userLogin/@name"/></param>
				<param name="Passwd"><xsl:value-of select="$userLogin/@password"/></param>
				<param name="source">livcos.org-test_contacts-1</param>
				<param name="service">cp</param>
				<param name="accountType">GOOGLE</param>
			</http-request>
		</xsl:variable>
		<test>
			<xsl:variable name="login" select="f:request($loginUrl, $loginRequest)"/>
			<xsl:variable name="auth"
				select="substring-after(tokenize($login/body, '\n')[substring-before(., '=') eq 'Auth'], '=')"/>
			<xsl:sequence select="$login"/>
			<auth><xsl:sequence select="$auth"/></auth>
			<xsl:variable name="feedRequest">
				<http-request>
					<header name="Authorization">GoogleLogin auth=<xsl:value-of select="$auth"/></header>
					<param name="max-results">10</param>
					<param name="alt">json</param>
				</http-request>
			</xsl:variable>
			<xsl:sequence select="f:request($feedUrl, $feedRequest)"/>
			<xsl:variable name="feedRequest2">
				<http-request>
					<header name="Authorization">GoogleLogin auth=<xsl:value-of select="$auth"/></header>
					<param name="max-results">10</param>
					<param name="alt">atom</param>
				</http-request>
			</xsl:variable>
			<xsl:sequence select="f:request($feedUrl, $feedRequest2)"/>
			<xsl:variable name="feedRequest3">
				<http-request>
					<header name="Authorization">GoogleLogin auth=<xsl:value-of select="$auth"/></header>
					<param name="max-results">10</param>
					<param name="alt">rss</param>
				</http-request>
			</xsl:variable>
			<xsl:sequence select="f:request($feedUrl, $feedRequest3)"/>
		</test>
	</xsl:template>
	
</xsl:stylesheet>